/**
 *
 * @class app.widgets.FormWin
 * @extends Ext.Window
 */

Ext.ns('app.widgets');

app.widgets.FormWin = Ext.extend(Ext.Window, {
 
	title: 'Title'
	,collapsible: true
	,maximizable: true
	,width: 400
	,height: 300
	,layout: 'fit'
	,plain: true
	,bodyStyle: 'padding:5px;'
	,buttonAlign: 'center'

		
	/**
	 * Scope for all the event handlers
	 * @cfg {Object} scope
	 */
		
	/**
	 * Form items
	 * @cfg {Object/Array} items
	 */
		
	/**
	 * @cfg {Function} submitFn
	 */
	
	/**
	 * @cfg {String} submitText
	 */
	
	/**
	 * @cfg {Function} cancelFn
	 */
	
	/**
	 * @cfg {String} cancelText
	 */
	
	/**
	 * True if the form uploads files
	 * @cfg {Boolean} fileUpload
	 */	
		
	/**
	 * @cfg {String} formId
	 */
	
	/**
	 * @cfg {String} formItems
	 */	
	
	/**
	 * Config object for a FormPanel
	 * 
	 * @cfg {Object} formConfig
	 */
		
    ,initComponent:function() {
         
    	this.form = this.createForm(this.initialConfig); 
        var config = {
        		items : [this.form]
        };
        
        Ext.apply(this, config);
        Ext.apply(this.initialConfig, config);

        app.widgets.FormWin.superclass.initComponent.apply(this, arguments);
         
    } 
	
	/*
	 * Getters & Setters
	 */

	, getFormPanel : function(){
		return this.form; 
	}

	/*
	 * Event handlers
	 */
	
	, onSubmit : function(btn){
		btn.findParentByType('form').getForm().submit({
			failure: this.onFailure, 
			success: this.onSuccess, 
			scope: this
		}); 
	}
	
	, onCancel : function(btn){
		btn.findParentByType('formwin').close(); 
	}
	
	, onFailure : function(form,action){
		switch (action.failureType) {
			case Ext.form.Action.CLIENT_INVALID:
				app.Msg.error('Form fields may not be submitted with invalid values.');
				break;
			case Ext.form.Action.CONNECT_FAILURE:
				app.Msg.error('Ajax communication failed.');
				break;
			case Ext.form.Action.SERVER_INVALID:
				var msg = 'Server eror'; 
				
				if(action.result){
					msg = action.result.msg ? action.result.msg : msg;  
				}
				
				app.Msg.error(msg);
				break; 
	   }
	}
	
	, onSuccess : function(form, action){
		var msg = 'Form successfully submitted.'; 
		
		if(action.result){
			msg = action.result.msg ? action.result.msg : msg;  
		}
		
		app.Msg.info(msg, 'Success');  
		this.close(); 
	}
	
	/*
	 * Utils
	 */
	
	, createForm : function(config){

		var scope = config.scope ? config.scope : this;  
		
		var formConfig = config.formConfig ? config.formConfig : {
			baseCls: 'x-plain',
			id: config.formId ? config.formId : Ext.id(),
			defaults: {anchor: '100%'},
		    defaultType: 'textfield',
			fileUpload: config.fileUpload ? true : false,
			buttons: [{
				text: config.submitText ? config.submitText : 'Submit', 
		    	handler: config.submitFn ? config.submitFn : this.onSubmit, 
		    	scope: scope
		    },{
		    	text: config.submitText ? config.submitText : 'Cancel',
		    	handler: config.cancelFn ? config.cancelFn : this.onCancel, 
		    	scope: scope
		    }], 
			items: config.formItems ? config.formItems : [], 
			api: config.formApi ? config.formApi : undefined,
			paramOrder: ['uid']
		};  
		
		return new Ext.form.FormPanel(formConfig);
		
	}

}); 
 
Ext.reg('formwin', app.widgets.FormWin); 